<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/1/24
  Time: 13:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="/WEB-INF/include-head.jsp" %>

<link rel="stylesheet" href="css/pagination.css"/>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<link rel="stylesheet" href="ztree/zTreeStyle.css">
<script src="ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="crowd/my-role.js"></script>

<script type="text/javascript">
    $(function () {
        //1.为分页操作准备初始化数据(全局变量)
        window.pageNum = 1;
        window.pageSize = 5;
        window.keyword = "";

        //2.调用执行分页的函数,显示分页效果
        generatePage();

        //3.给查询按钮绑定单击响应函数
        $("#searchBtn").click(function () {

            window.keyword = $("#keywordInput").val();
            //2.调用执行分页的函数,显示分页效果
            generatePage();

        });

        // 4.点击新增按钮打开模态框
        $("#showModelBtn").click(function () {
            $("#addModal").modal("show")
        });


//5.给新增模态框中的保存按钮绑定单击响应函数
        $("#saveRoleBtn").click(function () {
            //获取用户在文本框中输入的角色名称
            // #addModal表示找到整个模态框
            // 空格表示在后代元素中继续查找
            //[name=roleName]表示匹配name属性等于roleName的元素

            let roleName = $.trim($("#addModal [name=roleName]").val());

            $.ajax({
                url: "role/save.json",
                type: "post",
                data: {
                    name: roleName
                },
                dataType: "json",
                success: function (response) {
                    let {result} = response;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功");
                        window.pageNum = 999999;
                        generatePage();
                    }

                    if (result == "FAILED") {
                        layer.msg("操作失败" + response.message);
                    }

                },
                error: function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });
            //关闭模态框
            $("#addModal").modal('hide');

            $("#addModal [name=roleName]").val("");
        });
        // 6.给页面上的“铅笔”按钮绑定单击响应函数，目的是打开模态框
        // 传统的事件绑定方式只能在第一个页面有效，翻页后失效了
        // $(".penciBtn").click(function () {
        //     alert("aaaaa....")
        // })
        // 使用 jQuery 对象的 on()函数可以解决上面问题
        // ①首先找到所有“动态生成”的元素所附着的“静态”元素
        // ②on()函数的第一个参数是事件类型
        // ③on()函数的第二个参数是找到真正要绑定事件的元素的选择器
        // ③on()函数的第三个参数是事件的响应函数
        $("#rolePageBody").on("click", ".penciBtn", function () {
            $("#editModal").modal("show");

            let roleName = $(this).parent().prev().text();

            window.roleId = this.id;

            $("#editModal [name=roleName]").val(roleName);

        });

        ///7.给更新模态框中的更新按钮绑定单击响应函数
        $("#editRoleBtn").click(function () {
            let roleName = $("#editModal [name=roleName]").val();
            $.ajax({
                url: "role/update.json",
                type: "post",
                data: {
                    id: window.roleId,
                    name: roleName
                },
                dataType: "json",
                success: function (response) {
                    let {result} = response;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功");
                        generatePage();
                    }

                    if (result == "FAILED") {
                        layer.msg("操作失败" + response.message);
                    }
                },
                error: function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            })
            $("#editModal").modal("hide");
        })

        /*        //测试
                let roleArray = [{roleId:5,roleName:"aaa"},{roleId:6,roleName:"bbb"}];
                showConfirmModel(roleArray);*/

        $("#removeRoleBtn").click(function () {

            let requestBody = JSON.stringify(window.roleIdArray);
            $.ajax({
                url: "role/remove/id/array.json",
                type: "post",
                data: requestBody,
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                success: function (response) {
                    let {result} = response;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功");
                        generatePage();
                        $("#summaryBox").prop("checked", false)

                    }

                    if (result == "FAILED") {
                        layer.msg("操作失败" + response.message);
                    }
                },
                error: function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }

            });
            $("#confirmModel").modal('hide')
        });
        $("#rolePageBody").on("click", ".removeBtn", function () {
            let roleName = $(this).parent().prev().text();

            let roleArray = [{roleId: this.id, roleName: roleName}];
            showConfirmModel(roleArray);

        });

        // 10.给总的checkbox绑定单机响应事件
        $("#summaryBox").click(function () {
            let currentStatus = this.checked;

            $(".itemBox").prop("checked", currentStatus)

        })

        // 11.全选 全不选的反向操作
        $("#rolePageBody").on("click", ".itemBox", function () {
            //获取当前已经选中的.itemBox的数量
            let checkedBoxCount = $(".itemBox:checked").length;

            //获取全部.itemBox的数量
            let totalBoxCount = $(".itemBox").length;

            $("#summaryBox").prop("checked", checkedBoxCount == totalBoxCount)

        });

        // 12.给批量删除的按钮绑定单击响应函数
        $("#batchRemoveBtn").click(function () {

            let roleArray = [];
            //遍历当前选中的多选框
            $(".itemBox:checked").each(function () {
                //使用this引用当前遍历得到的多选框
                let roleId = this.id;

                let roleName = $(this).parent().next().text();

                roleArray.push({
                    roleId,
                    roleName
                })
            })
            //检查roleArray的长度是否为0
            if (roleArray.length <= 0) {
                layer.msg("请至少选择一个");
                return;
            }

            showConfirmModel(roleArray);
        })

        //13.给分配权限按钮绑定单击响应函数
        $("#rolePageBody").on("click", ".checkBtn", function () {

            window.id = this.id;
            $("#assignModal").modal("show");
            fillAuthTree();
        });
        // 14.给分配权限模态框中的“分配”按钮绑定单击响应函数
        $("#assignBtn").click(function () {
            // ①收集树形结构的各个节点中被勾选的节点
            // [1]声明一个专门的数组存放 id
            let authIdArray = [];

            let zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");

            let checkedNodes = zTreeObj.getCheckedNodes();

            for (let i = 0; i < checkedNodes.length; i++) {
                let checkedNode = checkedNodes[i];
                let authId = checkedNode.id;
                authIdArray.push(authId);
            }
            let requestBody = {
                authIdArray: authIdArray,
                //为了服务器端handler方法能够统一使用List<Integer>方式接收数据，roleId也存入数组
                roleId: [window.id]
            }
            requestBody = JSON.stringify(requestBody);
            console.log(requestBody)
            $.ajax({
                url: "assign/do/role/assign/auth.json",
                type: "post",
                dataType: "json",
                data:requestBody,
                contentType: "application/json;charset=UTF-8",
                success: function (response) {
                    console.log(response)
                    let {result} = response;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功");
                    }

                    if (result == "FAILED") {
                        layer.msg("操作失败" + response.message);
                    }
                },
                error: function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });
            $("#assignModal").modal("hide");
        })
    });
</script>
<body>

<%@include file="/WEB-INF/include-nav.jsp" %>

<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keywordInput" class="form-control has-success" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="button" id="searchBtn" class="btn btn-warning"><i
                                class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button type="button" id="batchRemoveBtn" class="btn btn-danger"
                            style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button type="button" id="showModelBtn" class="btn btn-primary" style="float:right;"><i
                            class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/modal-role-add.jsp" %>
<%@include file="/WEB-INF/modal-role-edit.jsp" %>
<%@include file="/WEB-INF/modal-role-confirm.jsp" %>
<%@include file="/WEB-INF/modal-role-assign-auth.jsp" %>
</body>
</html>

